<?php
namespace  App\Api\Services\Pay;
use App\Admin\Actions\Post\Restore;
use App\Api\Controllers\Pay\Alicpay;
use App\Api\Models\CmcOrderList;
use App\Api\Services\JsonService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Console\Input\Input;
use Symfony\Contracts\Service\Attribute\Required;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Log;
use function Symfony\Component\String\s;

class Alipay
{
    public static function aliIndex($orderNumber,$totalPrice,$testSubject)
    {
        $order = [
            'out_trade_no' => $orderNumber,//订单号
            'total_amount' => $totalPrice,//money
            'subject' => $testSubject,//商品名称
            'timeout_express'=>config('alipay.timeout_express')
        ];
        $alipay = Pay::alipay(config("alipay"))->web($order);
         CmcOrderList::where(['order_sn'=>$orderNumber])->update(['paid'=>1]);
        return $alipay->send();// laravel 框架中请直接 `return $alipay`
    }

    // 订单号  $data->out_trade_no;
    // 支付宝交易号：$data->trade_no
    // 订单总金额：$data->total_amount
    public    static function  return()
    {
        $data = Pay::alipay(config("alipay"))->verify(); // 是的，验签就这么简单！
         $dataTrade=[$data->out_trade_no,$data->trade_no,$data->total_amount];
        return JsonService::success('购买成功',$dataTrade,config('code.HTTP_OK'));

    }
    /**
     * Notes:支付宝异步回调
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public static function notifyUrl()
    {
        $alipay = Pay::alipay(config("alipay"));
//       开启事务
        DB::beginTransaction();
        try{
            $data = $alipay->verify();
            Log::debug('Alipay notify', $data->all());
            require_once "../../Controllers/Pay/Alicpay.php";
                //处理成功一定要返回 success 这7个字符组成的字符串，
                die('SUCCESS');//响应success表示业务处理成功，告知支付宝无需在异步通知
        } catch (\Exception $e) {
            DB::rollBack();
            $e->getMessage();
            die('ERROR');
        }
        return $alipay->success()->send();// laravel 框架中请直接 `return

    }

    //查询普通支付订单
    static public function ordinaryPayment($out_trade_no){
        $alipay = Pay::alipay(config("alipay"));
        $order = [
            'out_trade_no' => $out_trade_no,//订单号
        ];
         // $order = '1514027114';
       return $alipay->find($order);
    }
    //查询退款订单
    static public function checkRefund($out_trade_no,$refund_price){
        $alipay = Pay::alipay(config("alipay"));
            $order = [
                'out_trade_no' => $out_trade_no,//订单
                'refund_amount' => $refund_price,//金钱
        ];

      return  $alipay->find($order, 'refund');
    }
    //查询转账订单
    static public function checkTransfer($out_trade_no){
        $alipay = Pay::alipay(config("alipay"));
        $order = [
            'out_trade_no' => $out_trade_no,//订单
        ];
          return $alipay->find($order, 'transfer');
    }


    //退款炒作
    static   public function  outTradeNo($out_trade_no,$refund_amount){
        $alipay = Pay::alipay(config("alipay"));
        $order = [
            'out_trade_no' => $out_trade_no,
            'refund_amount' => $refund_amount,
        ];
        return $alipay->refund($order);
    }

}
